package com.aerozhonghuan.logisticsmall.utils;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.aerozhonghuan.logisticsmall.framework.base.MyAppliation;

/**
 * 描述:
 * 作者:zzw
 * 创建日期：2018/5/22
 */

public class DisplayUtil {

    private static float scale;

    /**
     * 获取屏幕的宽（PX）
     *
     * @return
     */
    public static int getWindowWith() {
        return getDisplay()[0];
    }

    /**
     * 获取屏幕的宽高（PX）
     *
     * @return int [] {宽， 高}
     */
    public static int[] getDisplay() {
        if (getContext() != null) {
            WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
            Display display = manager.getDefaultDisplay();
            return new int[]{display.getWidth(), display.getHeight()};
        }
        return new int[2];
    }

    private static Context getContext() {
        return MyAppliation.getApplication();
    }

    /**
     * 获取屏幕的高（PX）
     *
     * @return
     */
    public static int getWindowHeight() {
        return getDisplay()[1];
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(double dip) {
        if (scale <= 0) {
            Context context = getContext();
            if (getContext() == null) {
                return (int) dip;
            }
            scale = getContext().getResources().getDisplayMetrics().density;
        }
        return (int) (dip * scale + 0.5f);
    }

    /**
     * 获取屏幕长宽比
     *
     * @param context
     * @return
     */
    public static float getScreenRate(Context context) {
        Point P = getScreenMetrics(context);
        float H = P.y;
        float W = P.x;
        return (H / W);
    }

    /**
     * 获取屏幕宽度和高度，单位为px
     *
     * @param context
     * @return
     */
    public static Point getScreenMetrics(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int w_screen = dm.widthPixels;
        int h_screen = dm.heightPixels;
        return new Point(w_screen, h_screen);

    }

    /**
     * M�todo que devuelve el alto de la pantalla
     *
     * @param context Context de la aplicaci�n
     * @return int con la altura
     */
    public static int getScreenHeight(Context context) {

        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return display.getHeight();

    }

    @SuppressWarnings("deprecation")
    /**
     * M�todo que devuelve el ancho de la pantalla
     *
     * @param context Context de la aplicaci�n
     *
     * @return int con la ancho
     */
    public static int getScreenWidth(Context context) {

        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return display.getWidth();

    }
}
